#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: fu
"""

import Common
import Config

class Map:
    def __init__(self):
        self.allxpos = []
        self.allypos = []
        self.edges = []
        self.flowers = []
        self.cells = [] # all cells on the map
        self.targets = []
        
        self.i0 = 'img/Blocks/' + 'Grass 1' + '.png'
        self.i1 = 'img/Decorations/' + 'desert mariposa' + '.png' # edges
        self.i2 = 'img/Decorations/' + 'Pink Flower 1' + '.png' # flowers
        self.i3 = 'img/Blocks/' + 'Rainbow 5' + '.png' # targets
        
    def set(self, n, p = Config.MAP_Y, q = Config.MAP_X, m = Config.MARGIN):
        c = 0
        k = 0
        for j in range(p):
            for i in range(q):
                x = i * 40 - (k % 2) * 20
                y = j * 10
                if x < 0:
                    continue
                else:
                    self.allxpos.append(x)
                    self.allypos.append(y)
                    c += 1
            k += 1
        
        screen_size = (Config.SCREEN_X, Config.SCREEN_Y)
        screen = Common.pygame.display.set_mode(screen_size)
        screen.fill((255, 255, 255))
        
        for i in range(c):
            if self.allxpos[i] == 0 or self.allxpos[i] == (q - 1) * 40 \
                or self.allypos[i] == 0 or self.allypos[i] == (p - 1) * 10:
                color = self.i1
                self.edges.append((self.allxpos[i], self.allypos[i]))
            else:
                color = self.i0
            image = Common.modify_img(color)
            cell = Common.pygame.Rect(self.allxpos[i], self.allypos[i], 40, 40)
            self.cells.append(cell)
            screen.blit(image, cell)
            
        for c in range(3):
            y = Common.random_even(m * 2, p - m * 2)
            x = Common.random_even(m, q - m)
            l = Common.random.randint(1, min((p - m * 2 - y), (q - m - x)))
            color = self.i2
            
            for i in range(l):
                image = Common.modify_img(color)
                flower = Common.pygame.Rect(x * 20, y * 10, 40, 40)
                x += 1
                y += 1
                
                self.flowers.append(flower)
                
        for i in range(n):
            y = Common.random_even(m * 2, p - m * 2)
            x = Common.random_even(m, q - m)
            color = self.i3
            
            image = Common.modify_img(color)
            target = Common.pygame.Rect(x * 20, y * 10, 40, 40)
            
            self.targets.append(target)
            
        Common.pygame.image.save(screen, 'img/Maps/' + 'Map 1' + '.png')
        